/****************************************************************
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.     *
 *                                                              *
 * Copyright (C) 2009, Jun Li All rights reserved.              *
 * More about Jun{                                              *
 * Homepage: http://www.jpxx.org/                               *
 * Email   : lijun@jpxx.org                                     *
 * }                                                            *
 * SOFTWARE ENGINEERING COLLEGE OF SICHUAN UNIVERSITY           *
 *                                                              *
 ****************************************************************/
package org.jpxx.web.project;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.jpxx.util.FileUtils;
import org.jpxx.web.Constants;

/**
 * 项目Classpath
 * 
 * @author Jun Li lijun@jpxx.org (http://www.jpxx.org)
 * @version 1.0.0 $ org.jpxx.web.project.ProjectClasspath.java $ $ Date: 2009-5-15
 *          $
 */
public class ProjectClasspath {

	private Logger log = Logger.getLogger(ProjectClasspath.class);

	private LayerEnum[] layer = Constants.SYSTEM_LAYER;

	private String springResourceDir = Constants.SRC_RESOURCES_PATH
			+ "META-INF" + File.separator + "spring" + File.separator;

	/**
	 * 获取classpath路径
	 * 
	 * @return 临时classpath路径
	 */
	public String get() {
		String classes = Constants.CLASSPATH + "classes" + File.separator;
		File f = new File(classes);

		if (f.exists()) {
			try {
				log.info("clear temp classpath :" + classes);
				FileUtils.delete(classes);
			} catch (IOException e) {
				log.error("clear temp classpath failed.");
			}
		} else {
			f.mkdirs();
		}

		String spring[] = getSpringResources();
		String resource[] = getResourcesDirectory();

		if (spring != null) {
			for (int i = 0; i < spring.length; i++) {
				try {
					FileUtils.copyFile(spring[i], classes + File.separator
							+ "spring" + File.separator);
					log.debug("load spring xml file: "
							+ new File(spring[i]).getName());
				} catch (IOException e) {
					log.error("copy file " + spring[i] + " error.");
				}
			}
		}

		if (resource != null) {
			for (int i = 0; i < resource.length; i++) {
				try {

					File temp = new File(resource[i]);
					if (temp.isFile()) {
						FileUtils.copyFile(resource[i], classes);
						log.debug("load resource file: " + temp.getName());
					} else if (!temp.getName().startsWith(".")) {
						FileUtils.copy(resource[i], classes + temp.getName());
						log.debug("load resource directory: " + temp.getName());
					}
				} catch (IOException e) {
					log.error("copy file " + resource[i] + " error.");
				}
			}
		}
		return classes;
	}

	/**
	 * 获取Spring资源文件
	 * 
	 * @return Spring资源文件路径
	 */
	private String[] getSpringResources() {

		List<String> list = new ArrayList<String>();

		for (int i = 0; i < layer.length; i++) {
			String layerPath = Constants.PROJECT_PATH + Constants.PROJECT_NAME
					+ File.separator + layer[i].toString().toLowerCase()
					+ File.separator;
			File files[] = new File(layerPath).listFiles();
			if (files != null) {
				for (int j = 0; j < files.length; j++) {
					File f = files[j];
					// 找子工程
					if (f.isDirectory() && !f.getName().startsWith(".")) {
						String projectPath = layerPath + f.getName()
								+ File.separator;

						// 资源文件目录
						String resourceDir = projectPath + springResourceDir;

						File springFile[] = new File(resourceDir).listFiles();
						if (springFile == null) {
							continue;
						}
						for (int m = 0; m < springFile.length; m++) {
							if (springFile[m].getName().endsWith(".xml")) {
								list.add(springFile[m].getAbsolutePath());
							}
						}
					}
				}
			}
		}

		return (String[]) list.toArray(new String[0]);
	}

	/**
	 * 获取各个子项目的资源文件夹路径。
	 * 
	 * @return 资源文件夹路径
	 */
	private String[] getResourcesDirectory() {

		List<String> list = new ArrayList<String>();
		for (int i = 0; i < layer.length; i++) {
			String layerPath = Constants.PROJECT_PATH + Constants.PROJECT_NAME
					+ File.separator + layer[i].toString().toLowerCase()
					+ File.separator;
			File files[] = new File(layerPath).listFiles();
			if (files != null) {
				for (int j = 0; j < files.length; j++) {
					File f = files[j];
					// 找子工程
					if (f.isDirectory() && !f.getName().startsWith(".")) {
						String projectPath = layerPath + f.getName()
								+ File.separator;

						// 资源文件目录
						String resourceDir = projectPath
								+ Constants.SRC_RESOURCES_PATH;

						File temp[] = new File(resourceDir).listFiles();
						for (int k = 0; k < temp.length; k++) {
							File tempfile = temp[k];
							if (tempfile.getName().equals("META-INF")
									|| tempfile.getName().startsWith(".")) {
								continue;
							} else {
								list.add(tempfile.getAbsolutePath());

							}
						}
					}
				}
			}
		}

		return (String[]) list.toArray(new String[0]);
	}

}
